import {Injectable} from '@angular/core';
import {ExecActionInterface} from "./exec-action-interface";
import {ParamDef} from "../model/param-def";
import {NameValue} from "../model/name-value";
import {ParamDefType} from "../model/param-def-type";
import {FileInfo} from "../model/file-info";

@Injectable({
  providedIn: 'root'
})
export class MatchExecActionService extends ExecActionInterface{

  public readonly matchItem:ParamDef;
  private readonly matchWhere: ParamDef;
  private readonly matchValue: ParamDef;

  constructor() {
    super("匹配", "match");
    this.matchItem =new ParamDef(ParamDefType.Select, "matchItem")
    this.matchWhere =new ParamDef(ParamDefType.Select, "matchWhere")
    this.matchValue =new ParamDef(ParamDefType.Text, "matchValue")
    this.paramsDef=[
      this.matchItem,this.matchWhere,this.matchValue
    ]

    this.addMatchItem("文件名","name")
    this.addMatchItem("文件扩展名","ext")
    this.addMatchItem("文件创建时间","createTime")
    this.addMatchItem("文件最后修改时间","lastTime")
    this.addMatchItem("文件路径","filePath")

    this.matchWhere.addNameValue("包含","in")
    this.matchWhere.addNameValue("不包含","notIn")
    this.matchWhere.addNameValue("相等","eq")
    this.matchWhere.addNameValue("不相等","neq")
    this.matchWhere.addNameValue("大于","gt")
    this.matchWhere.addNameValue("大于等于","gte")
    this.matchWhere.addNameValue("小于","lt")
    this.matchWhere.addNameValue("小于等于","lte")
    this.matchWhere.addNameValue("正则匹配","regex")
    this.matchWhere.addNameValue("脚本匹配","script")
  }

  public addMatchItem(text:string,code:string) {
    this.matchItem.addNameValue(text,code)
  }


  exec(fileInfo: FileInfo, lastVal: any, param: any) {
    let checkVal = eval("fileInfo."+param.matchItem)

    let matchRs = eval(`this.${param.matchWhere}(checkVal,param.matchValue)`)

    return matchRs;
  }

  notIn(checkVal:any,matchVal:string){
    for (let it of matchVal.split(",")) {
      if (it == checkVal){
        return false
      }
    }
    return true;
  }

  in(checkVal:any,matchVal:string){
    for (let it of matchVal.split(",")) {
      if (it == checkVal){
        return true
      }
    }
    return false;
  }



}
